﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace App.Store
{
    public partial class frmSampleLend : Form
    {
        Entities db = new Entities();

        T_Lend m_Lend = null;

        T_Sample m_Sample = null;

        List<T_Sample> tSample = null;

        T_LendDetail lendDetail = null;

        public int[] bCount { get; set; }

        private bool IsQuery = true;

        public string yName { get; set; }

        public string lNo
        {
            set
            {
                var allLendList = db.T_Lend.Join(db.T_LendDetail, a => a.lID, b => b.lID, (a, b) => new { a, b }).Join(db.T_Sample, c => c.b.xID, d => d.xID, (c, d) => new {c.a.lNo, d.productNo, c.a.opDate, d.size2, d.sKind, d.brand, d.property, d.season, d.contractNo, d.sCount, c.b.lCount, c.b.lendToOut, c.b.lReturn, d.location, c.b.commandName, c.a.cust });
                var sampleList = allLendList.Where(a => a.lNo == value).ToList();
                bs2.DataSource = sampleList;

                var lendList = db.T_Lend.Where(a => a.lNo == value).ToList();
                foreach (T_Lend lend in lendList)
                {
                    m_Lend = lend;
                    break;
                }

                if (m_Lend == null) return;

                txtDetaillNo.Text = m_Lend.lNo;
                dt1.ValueText = m_Lend.requireTime.ToString();
                dt2.ValueText = m_Lend.lendTime.ToString();
                if (!string.IsNullOrEmpty(m_Lend.bFinished.ToString()))
                {
                    if (bool.Parse(m_Lend.bFinished.ToString())) checkBoxIsfinish.Checked = true;
                }
                txtcName.Text = m_Lend.cName;
                txtyName.Text = m_Lend.yName;
                cbDCust.Text = m_Lend.cust;
            }
        }

        public int[] xID
        {
            set
            {
                tSample = new List<T_Sample>();
                for (int i = 0; i < value.Length; i++)
                {
                    int id = value[i];
                    var list = db.T_Sample.Where(a => a.xID == id);

                    tSample.Add(list.FirstOrDefault());
                }
                bs2.DataSource = tSample;

                string str = DateTime.Now.ToString("yyMMdd");
                var count = db.T_Lend.Where(a => a.lNo.Contains(str)).Count();
                if (count < 9)
                {
                    txtDetaillNo.Text = str + "-0" + (count + 1).ToString();
                }
                else
                {
                    txtDetaillNo.Text = str + "-" + (count + 1).ToString();
                }
                dt1.ValueText = DateTime.Now.ToString("yy/MM/dd");
                dt2.ValueText = DateTime.Now.ToString("yy/MM/dd"); 
                txtyName.Text = this.yName;
                txtcName.Text = Startup.GetApp().CurrentUser.name;
               

            }
        }

        public frmSampleLend()
        {
            InitializeComponent();
            panList.BringToFront();
            gvDataSource();
        }

        private void gvDataSource()
        {
            DateTime dt=DateTime.Now.Date;
            var sendList = db.T_Lend.Where(a=>a.requireTime==dt).OrderByDescending(b=>b.lNo).ToList();
            bs1.DataSource = sendList;

            bnav1.BindingSource = bs1;
            bnav2.BindingSource = bs2;
            gvList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            gvDetail.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void gvID()
        {

            if (gvList.RowCount > 0)
            {
                int index = gvList.CurrentRow.Index;    //取得选中行的索引  
                this.lNo = gvList.Rows[index].Cells["collNo"].Value.ToString();
            }
            else
            {
                
            }

        }

        public void ShowPanDetail()
        {
            panDetail.BringToFront();
        }
        private void btnDetail_Click(object sender, EventArgs e)
        {
            gvID();
            if (this.IsQuery)
            {
                this.panDetail.BringToFront();
                this.btnDetail.Text = "概要";
                this.IsQuery = false;

            }
            else
            {
                this.panList.BringToFront();
                this.btnDetail.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void gvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.lNo = gvList.Rows[e.RowIndex].Cells["collNo"].Value.ToString();
                this.panDetail.BringToFront();
                this.btn1.Text = "概要";
                this.IsQuery = false;
            }
            catch
            {
                Msg.ShowError("请选择表里的数据！");
            }
        }

        public void SaveAdd()
        {
            m_Lend = new T_Lend();
            if (db.T_Lend.Count() <= 0) m_Lend.lID = 1;
            else m_Lend.lID = db.T_Lend.OrderByDescending(a => a.lID).First().lID + 1;
            m_Lend.lNo=txtDetaillNo.Text;
            m_Lend.requireTime = dt1.Value;
            m_Lend.lendTime=dt2.Value;
            if (checkBoxIsfinish.Checked == true)
            {
                m_Lend.bFinished = true;
            }
            m_Lend.opName = Startup.GetApp().CurrentUser.name;
            m_Lend.opDate = DateTime.Now.ToLocalTime();
            m_Lend.cName = txtcName.Text;
            m_Lend.yName = this.yName;
            m_Lend.cust = cbDCust.Text;

            db.T_Lend.Add(m_Lend);
            int i = db.SaveChanges();

            try
            {
                int j = 0;
                foreach (var list in tSample)
                {
                    m_Sample = list;
                    lendDetail = new T_LendDetail();
                    lendDetail.ldID = db.T_LendDetail.OrderByDescending(a => a.ldID).First().ldID + 1;
                    lendDetail.lID = m_Lend.lID;
                    lendDetail.xID = m_Sample.xID;
                    lendDetail.lCount = bCount[j];
                    lendDetail.srCount = m_Sample.srCount;
                    lendDetail.oCount = m_Sample.oCount;
                    lendDetail.pCount = m_Sample.pCount;
                    //lendDetail.reMark=
                    lendDetail.lReturn = 0;
                    lendDetail.srReturn = 0;
                    lendDetail.lendToOut = 0;
                    lendDetail.srToOut = 0;
                    lendDetail.commandName = this.yName;
                    lendDetail.productNoImg = m_Sample.productNoImg;
                    db.T_LendDetail.Add(lendDetail);
                    db.SaveChanges();
                    j++;
                }
            }
            catch(Exception e)
            {
                Msg.ShowError(e.Message);
            }
            Msg.ShowInfo(string.Format("已保存{0}条单号信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        public void SaveModify()
        {
            if (m_Lend == null) return;

            m_Lend.lNo = txtDetaillNo.Text;
            if (m_Lend.requireTime != null) m_Lend.requireTime = Convert.ToDateTime(dt1.ValueText);
            if (m_Lend.lendTime != null) m_Lend.lendTime = Convert.ToDateTime(dt2.ValueText);
            if (checkBoxIsfinish.Checked == true)
            {
                m_Lend.bFinished = true;
            }
            m_Lend.cName = txtcName.Text;
            m_Lend.yName = txtyName.Text;
            m_Lend.cust = cbDCust.Text;

            int i = db.SaveChanges();

            Msg.ShowInfo(string.Format("已保存{0}条单号信息。", i));
            this.panList.BringToFront();
            this.btn1.Text = "明细";
            this.IsQuery = true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("您确信保存单号信息吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                if (m_Lend == null)
                {
                    SaveAdd();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
                else
                {
                    SaveModify();
                    gvDataSource();
                    this.panList.BringToFront();
                    this.btn1.Text = "明细";
                    this.IsQuery = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (Msg.ShowWarning("尚未保存，您确定要取消吗？") == System.Windows.Forms.DialogResult.Yes)
            {
                this.panList.BringToFront();
                this.btn1.Text = "明细";
                this.IsQuery = true;
            }
        }

        private void frmSampleLend_Load(object sender, EventArgs e)
        {
            Service.Cust(cbcust);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnDetail_Click(null, null);
        }
    }
}
